package com.ruoyi.coupon.service.impl;

import com.ruoyi.common.core.flashpay.utils.IdWorker;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.coupon.domain.TbCoupon;
import com.ruoyi.coupon.domain.TbCouponType;
import com.ruoyi.coupon.mapper.TbCouponTypeMapper;
import com.ruoyi.coupon.service.ITbCouponTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 优惠券类型Service业务层处理
 *
 * @author gzc
 * @date 2023-04-24
 */
@Service
public class TbCouponTypeServiceImpl implements ITbCouponTypeService {
    @Autowired
    private TbCouponTypeMapper tbCouponTypeMapper;

    @Autowired
    private IdWorker idWorker;

    /**
     * 查询优惠券类型
     *
     * @param couponTypeId 优惠券类型主键
     * @return 优惠券类型
     */
    @Override
    public TbCouponType selectTbCouponTypeByCouponTypeId(Long couponTypeId) {
        return tbCouponTypeMapper.selectTbCouponTypeByCouponTypeId(couponTypeId);
    }

    /**
     * 查询优惠券类型列表
     *
     * @param tbCouponType 优惠券类型
     * @return 优惠券类型
     */
    @Override
    public List<TbCouponType> selectTbCouponTypeList(TbCouponType tbCouponType) {
        return tbCouponTypeMapper.selectTbCouponTypeList(tbCouponType);
    }

    /**
     * 新增优惠券类型
     *
     * @param tbCouponType 优惠券类型
     * @return 结果
     */
    @Transactional
    @Override
    public int insertTbCouponType(TbCouponType tbCouponType) {
        tbCouponType.setCouponTypeId(idWorker.nextId());
        int rows = tbCouponTypeMapper.insertTbCouponType(tbCouponType);
        insertTbCoupon(tbCouponType);
        return rows;
    }

    /**
     * 修改优惠券类型
     *
     * @param tbCouponType 优惠券类型
     * @return 结果
     */
    @Transactional
    @Override
    public int updateTbCouponType(TbCouponType tbCouponType) {
        tbCouponTypeMapper.deleteTbCouponByCouponTypeId(tbCouponType.getCouponTypeId());
        insertTbCoupon(tbCouponType);
        return tbCouponTypeMapper.updateTbCouponType(tbCouponType);
    }

    /**
     * 批量删除优惠券类型
     *
     * @param couponTypeIds 需要删除的优惠券类型主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTbCouponTypeByCouponTypeIds(Long[] couponTypeIds) {
        tbCouponTypeMapper.deleteTbCouponByCouponTypeIds(couponTypeIds);
        return tbCouponTypeMapper.deleteTbCouponTypeByCouponTypeIds(couponTypeIds);
    }

    /**
     * 删除优惠券类型信息
     *
     * @param couponTypeId 优惠券类型主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTbCouponTypeByCouponTypeId(Long couponTypeId) {
        tbCouponTypeMapper.deleteTbCouponByCouponTypeId(couponTypeId);
        return tbCouponTypeMapper.deleteTbCouponTypeByCouponTypeId(couponTypeId);
    }

    /**
     * 新增优惠券信息
     *
     * @param tbCouponType 优惠券类型对象
     */
    public void insertTbCoupon(TbCouponType tbCouponType) {
        List<TbCoupon> tbCouponList = tbCouponType.getTbCouponList();
        Long couponTypeId = tbCouponType.getCouponTypeId();
        if (StringUtils.isNotNull(tbCouponList)) {
            List<TbCoupon> list = new ArrayList<TbCoupon>();
            for (TbCoupon tbCoupon : tbCouponList) {
                tbCoupon.setCouponTypeId(couponTypeId);
                list.add(tbCoupon);
            }
            if (list.size() > 0) {
                tbCouponTypeMapper.batchTbCoupon(list);
            }
        }
    }
}
